package cn.lyvust.eais.manage.controller;

import cn.lyvust.eais.common.shiro.jwt.JwtToken;
import cn.lyvust.eais.common.util.MD5EncryptUtil;
import cn.lyvust.eais.manage.entity.Teacher;
import cn.lyvust.eais.manage.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import cn.lyvust.eais.common.annotation.JwtClaim;
import cn.lyvust.eais.common.annotation.SysLogs;
import cn.lyvust.eais.common.bean.ResponseCode;
import cn.lyvust.eais.common.bean.ResponseResult;
import cn.lyvust.eais.common.exception.RequestException;
import cn.lyvust.eais.common.util.BeansUtil;
import cn.lyvust.eais.common.util.JwtUtil;
import cn.lyvust.eais.manage.dto.param.SignInParam;
import cn.lyvust.eais.manage.dto.param.UserMainParam;
import cn.lyvust.eais.manage.dto.result.LoginResp;
import cn.lyvust.eais.manage.dto.result.UserMainResp;
import cn.lyvust.eais.manage.entity.ExpertInfo;
import cn.lyvust.eais.manage.entity.UserMain;
import cn.lyvust.eais.manage.service.ExpertInfoService;
import cn.lyvust.eais.manage.service.UserMainService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Licoy
 * @version 2018/4/13/14:02
 */
@RestController
@RequestMapping(value = {"/account"})
@Api(tags = {"账户相关"})
public class AccountController {
    @Autowired
    private UserMainService userService;
    @Autowired
    private TeacherService teacherService;

    @PostMapping(value = {"/sign-in"})
    @ApiOperation(value = "登录")
    @SysLogs("登录")
    public ResponseResult signIn(@RequestBody @Validated @ApiParam(value = "登录数据",required = true) SignInParam signInParam){
//        UserMain user = userService.findUserByName(signInParam.getUsername(), false);
//        boolean retFlag = JwtUtil.validatePassword(signInParam.getPassword(), user.getPassword());
//        if (!retFlag) {
//            throw RequestException.fail("用户密码不对,请重新输入!");
//        }

//        userService.signIn(signInParam);
//        LoginResp result = new LoginResp();
//        BeansUtil.copyProperties(SecurityUtils.getSubject().getPrincipal(), result);
//
//        ExpertInfo expertInfo = expertInfoService.getOne(new QueryWrapper<ExpertInfo>().eq("user_id", user.getId()));
//        if (expertInfo != null) {
//            result.setExpertId(expertInfo.getId());
//        }
//        return ResponseResult.e(ResponseCode.SIGN_IN_OK, result);

        userService.signIn(signInParam);
        return ResponseResult.e(ResponseCode.SIGN_IN_OK,((JwtToken)SecurityUtils.getSubject().getPrincipal()).getToken());
    }
    @PostMapping(value = {"/login"})
    @ApiOperation(value = "登录")
    @SysLogs("登录")
    public ResponseResult login(@RequestBody @Validated @ApiParam(value = "登录数据",required = true) SignInParam signInParam){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", signInParam.getUsername());
        List<Teacher> teacherList = teacherService.list(wrapper);
        if (teacherList.size() != 1) {
            throw RequestException.fail("登陆账号有误,请重新输入!");
        }
        Teacher teacher = teacherList.get(0);
        String encryptPW = MD5EncryptUtil.encrypt(signInParam.getPassword() + signInParam.getUsername());
        if (!encryptPW.equals(teacher.getPassword())) {
            throw RequestException.fail("密码有误,请重新输入!");
        }
        return ResponseResult.e(ResponseCode.SIGN_IN_OK, teacher);
    }
    @PostMapping(value = "/list")
    @ApiOperation(value = "获取用户信息")
    @SysLogs("获取用户信息")
    public ResponseResult<List<UserMainResp>> list(UserMainParam param){
        return ResponseResult.e(ResponseCode.OK, userService.list(param));
    }

    @PostMapping(value = "/current")
    @ApiOperation(value = "获取当前用户信息")
    @SysLogs("获取当前用户信息")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "Authorization", value = "", required = true) })
    public ResponseResult current(){
        return ResponseResult.e(ResponseCode.OK, userService.getCurrentUser());
    }

    @PostMapping(value = "/logout")
    @ApiOperation(value = "注销登录")
    @SysLogs("注销登录")
    public ResponseResult logout(){
        try {
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
        }catch (Exception e){
            return ResponseResult.e(ResponseCode.LOGOUT_FAIL);
        }
        return ResponseResult.e(ResponseCode.LOGOUT_OK);
    }

    @PostMapping(value = "/all-permission-tag")
    @ApiOperation(value = "获取所有的权限标示")
    public ResponseResult<List<String>> getAllPermissionTag(@JwtClaim String t){
        return ResponseResult.e(ResponseCode.OK, userService.getAllPermissionTag());
    }

}
